//
//  SINAPickDateView.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/22.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAPickDateView.h"


@interface SINAPickDateView()<UIPickerViewDataSource, UIPickerViewDelegate>
/** 选择的年 */
@property (nonatomic, assign)NSInteger selectYear;
/** 选择的月 */
@property (nonatomic, assign)NSInteger selectMonth;
/** 选择的日 */
@property (nonatomic, assign)NSInteger selectDay;
/** 选择的时 */
@property (nonatomic, assign)NSInteger selectHour;
/** 选择的分 */
@property (nonatomic, assign)NSInteger selectMin;
/** 选择的秒 */
@property (nonatomic, assign)NSInteger selectSec;

@property (nonatomic, assign)NSInteger currentYear;
@property (nonatomic, assign)NSInteger currentMonth;
@property (nonatomic, assign)NSInteger currentDay;
@property (nonatomic, assign)NSInteger defaultYear;
@property (nonatomic, assign)NSInteger defaultMonth;
@property (nonatomic, assign)NSInteger defaultDay;
@property (nonatomic, assign)NSInteger defaultHousr;
@property (nonatomic, assign)NSInteger defaultMin;
@property (nonatomic, assign)NSInteger defaultSecond;

@property (nonatomic, assign)NSInteger minShowYear;
@property (nonatomic, assign)NSInteger yearSum;
@end

@implementation SINAPickDateView
@dynamic delegate;

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (void)initPickView
{
    [super initPickView];
    //最小年份
    _minShowYear = 1990;
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 指定获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags fromDate:dt];
    
    _yearSum = comp.year-_minShowYear+1;
    _currentYear=comp.year;
    _currentMonth=comp.month;
    _currentDay=comp.day;
    
    
    
    _selectYear  = comp.year;
    _selectMonth = comp.month;
    _selectDay   = comp.day;
    _selectHour = comp.hour;
    _selectMin=comp.minute;
    _selectSec=comp.second;
    
    _defaultYear = comp.year;
    _defaultMonth = comp.month;
    _defaultDay=comp.day;
    _defaultHousr=comp.hour;
    _defaultMin=comp.minute;
    _defaultSecond=comp.second;
    
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
}
//设定默认显示的日期
-(void)setDefaultTSelectYear:(NSInteger)defaultSelectYear defaultSelectMonth:(NSInteger)defaultSelectMonth defaultSelectDay:(NSInteger)defaultSelectDay defaultSelectHour:(NSInteger)defaultSelectHour{
    
    if (defaultSelectYear!=0) {
        _defaultYear=defaultSelectYear;
    }
    
    if (defaultSelectMonth!=0) {
        _defaultMonth = defaultSelectMonth;
    }
    
    if (defaultSelectDay!=0) {
        _defaultDay=defaultSelectDay;
    }
    
    if (defaultSelectHour!=0) {
        _defaultHousr=defaultSelectHour;
    }
    
    if (defaultSelectYear==-1) {
        _defaultYear=_currentYear+1;
        _defaultMonth=1;
        _defaultDay=1;
    }
    
    
    [self.pickerView selectRow:(_defaultYear - _minShowYear) inComponent:0 animated:NO];
    [self.pickerView reloadComponent:1];
    [self.pickerView selectRow:(_defaultMonth - 1) inComponent:1 animated:NO];
    //显示日
    if (_isShowDay==YES) {
        if (_isShowHour) {
            //显示时
            [self.pickerView reloadComponent:3];
            [self.pickerView selectRow:_defaultDay inComponent:1 animated:NO];
            [self.pickerView selectRow:_defaultHousr inComponent:2 animated:NO];
        } else {
            //不显示时
            [self.pickerView reloadComponent:2];
            [self.pickerView selectRow:_defaultDay inComponent:1 animated:NO];
        }
    }else{
        //不显示日
        if (_isShowHour) {
            //显示时
            [self.pickerView reloadComponent:2];
            [self.pickerView selectRow:_defaultHousr inComponent:1 animated:NO];
        }
    }
    
    [self refreshPickViewData];
    
}
-(void)setDefaultTSelectYear:(NSInteger)defaultSelectYear defaultSelectMonth:(NSInteger)defaultSelectMonth defaultSelectDay:(NSInteger)defaultSelectDay defaultSelectHour:(NSInteger)defaultSelectHour defaultSelectMin:(NSInteger)defaultSelectMin defaultSelectSec:(NSInteger)defaultSelectSec{
    
    if (defaultSelectYear!=0) {
        _defaultYear=defaultSelectYear;
    }
    
    if (defaultSelectMonth!=0) {
        _defaultMonth = defaultSelectMonth;
    }
    
    if (defaultSelectDay!=0) {
        _defaultDay=defaultSelectDay;
    }
    
    if (defaultSelectHour!=0) {
        _defaultHousr=defaultSelectHour;
    }
    if (defaultSelectMin!=0) {
        _defaultMin=defaultSelectMin;
    }
    if (defaultSelectSec!=0) {
        _defaultSecond=defaultSelectSec;
    }
    
    if (defaultSelectYear==-1) {
        _defaultYear=_currentYear+1;
        _defaultMonth=1;
        _defaultDay=1;
    }
    
    
    [self.pickerView selectRow:(_defaultYear - _minShowYear) inComponent:0 animated:NO];
    [self.pickerView reloadComponent:1];
    [self.pickerView selectRow:(_defaultMonth - 1) inComponent:1 animated:NO];
    //显示日
    if (_isShowDay==YES) {
        if (_isShowHour) {
            //显示时
            
            //显示分 秒
            if (_isShowSecond&&_isShowMin) {
                [self.pickerView reloadComponent:5];
                [self.pickerView selectRow:_defaultDay inComponent:1 animated:NO];
                [self.pickerView selectRow:_defaultHousr inComponent:2 animated:NO];
                [self.pickerView selectRow:_defaultMin inComponent:3 animated:NO];
                [self.pickerView selectRow:_defaultSecond inComponent:4 animated:NO];
                
            }else{
                [self.pickerView reloadComponent:3];
                [self.pickerView selectRow:_defaultDay inComponent:1 animated:NO];
                [self.pickerView selectRow:_defaultHousr inComponent:2 animated:NO];
                
            }
        
        } else {
            //不显示时
            [self.pickerView reloadComponent:2];
            [self.pickerView selectRow:_defaultDay inComponent:1 animated:NO];
        }
    }else{
        //不显示日
        if (_isShowHour) {
            //显示时
            [self.pickerView reloadComponent:2];
            [self.pickerView selectRow:_defaultHousr inComponent:1 animated:NO];
        }
    }
    
    [self refreshPickViewData];
    
}
//是否显示至今选项
-(void)setIsAddYetSelect:(BOOL)isAddYetSelect{
    _isAddYetSelect = isAddYetSelect;
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //判断是否要显示日，如果显示则返回3列，反之返回2列
    if (_isShowDay==YES) {
        if (_isShowSecond&&_isShowMin) {
            return 6;
        }else{
        return 4;
        }
    }else{
        return 2;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        if (_isAddYetSelect==YES) {
            //显示至今选项的话，需要比总共要显示的年份多返回一行
            return self.yearSum+1;
        }else{
            return self.yearSum;
        }
    }else if(component == 1) {
        NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear;
        if (yearSelected==_currentYear+1) {
            //至今选项的时候月份信息不返回
            return 0;
        }else{
            
            return 12;
        }
    }else {
        
        if (_isShowHour) {
            if (component==3) {
                return 24;
            }
        }
        if (_isShowSecond&&_isShowMin) {
            if (component==4) {
                return 60;
            }else if (component==5) {
                return 60;
            }
        }
        NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear;
        if (yearSelected==_currentYear+1) {
            //至今选项的时候日信息不返回
            return 0;
        }else{
            NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear;
            NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
            return  [self getDaysWithYear:yearSelected month:monthSelected];
        }
    }
    
    
}
//根据年、月判断日期天数
- (NSInteger)getDaysWithYear:(NSInteger)year
                       month:(NSInteger)month
{
    switch (month) {
        case 1:
            return 31;
            break;
        case 2:
            if (year%400==0 || (year%100!=0 && year%4 == 0)) {
                return 29;
            }else{
                return 28;
            }
            break;
        case 3:
            return 31;
            break;
        case 4:
            return 30;
            break;
        case 5:
            return 31;
            break;
        case 6:
            return 30;
            break;
        case 7:
            return 31;
            break;
        case 8:
            return 31;
            break;
        case 9:
            return 30;
            break;
        case 10:
            return 31;
            break;
        case 11:
            return 30;
            break;
        case 12:
            return 31;
            break;
        default:
            return 0;
            break;
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    //每一行的高度
    return 48;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    NSInteger selectYear;
    NSInteger selectMonth;
    NSInteger selectHoure;
    
    
    switch (component) {
        case 0:
            [pickerView reloadComponent:1];
            if (_isAddYetSelect==YES) {
                selectYear = row+_minShowYear+1;
            }else{
                selectYear = row+_minShowYear;
            }
            if (_isShowDay==YES) {
                [pickerView reloadComponent:3];
            }
            break;
        case 1:
            selectMonth = row+1;
            if (_isShowDay==YES) {
                [pickerView reloadComponent:3];
            }
        case 3:
            selectHoure = row+1;
            if (_isShowDay==YES) {
                [pickerView reloadComponent:3];
            }
        default:
            break;
    }
    
    [self refreshPickViewData];
    
}



- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    
    NSString *text;
    if (component == 0) {
        
        if (_isAddYetSelect==YES) {
            
            if (row+_minShowYear==_currentYear+1) {
                text=@"至今";
            }else{
                text =  [NSString stringWithFormat:@"%zd年", row + _minShowYear];
            }
        }else{
            text =  [NSString stringWithFormat:@"%zd年", row + _minShowYear];
        }
        
    }else if (component == 1){
        if (_isAddYetSelect==YES) {
            NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear;
            if (yearSelected==_currentYear+1) {
                text =  [NSString stringWithFormat:@""];
            }else{
                NSInteger month = row+1;
                if (month<=9) {
                    text =  [NSString stringWithFormat:@"0%zd月", row + 1];
                } else {
                    text =  [NSString stringWithFormat:@"%zd月", row + 1];
                }
            }
        }else{
            NSInteger month = row+1;
            if (month<=9) {
                text =  [NSString stringWithFormat:@"0%zd月", row + 1];
            } else {
                text =  [NSString stringWithFormat:@"%zd月", row + 1];
            }
        }
        
        
    }else if (component == 3){
        NSInteger hour=row + 1;
        if (hour<=9) {
            text = [NSString stringWithFormat:@"0%zd时", row + 1];
        }else{
            text = [NSString stringWithFormat:@"%zd时", row + 1];
        }
    }else if (component == 4){
        NSInteger hour=row + 1;
        if (hour<=9) {
            text = [NSString stringWithFormat:@"0%zd分", row + 1];
        }else{
            text = [NSString stringWithFormat:@"%zd分", row + 1];
        }
    }else if (component == 5){
        NSInteger hour=row + 1;
        if (hour<=9) {
            text = [NSString stringWithFormat:@"0%zd秒", row + 1];
        }else{
            text = [NSString stringWithFormat:@"%zd秒", row + 1];
        }
    }else{
        NSInteger day = row+1;
        if (day<=9) {
            text = [NSString stringWithFormat:@"0%zd日", row + 1];
        } else {
            text = [NSString stringWithFormat:@"%zd日", row + 1];
        }
        
    }
    
    

    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    [label setBackgroundColor:[UIColor whiteColor]];
    label.font = [UIFont systemFontOfSize:16];
    label.text = text;
    
    
    
    return label;
}


- (void)clickConfirmButton
{
    
    
    if ([self.delegate respondsToSelector:@selector(pickerDateView:selectYear:selectMonth:selectDay:selectHours:)]) {
        
        [self.delegate pickerDateView:self selectYear:self.selectYear selectMonth:self.selectMonth selectDay:self.selectDay selectHours:self.selectHour];
        
    }
    
    if ([self.delegate respondsToSelector:@selector(pickerDateView:selectYear:selectMonth:selectDay:selectHours:selectMins:selectSecond:)]) {
        [self.delegate pickerDateView:self selectYear:self.selectYear selectMonth:self.selectMonth selectDay:self.selectDay selectHours:self.selectHour selectMins:self.selectMin selectSecond:self.selectSec];
    }
    
    [super clickConfirmButton];
    
}



- (void)refreshPickViewData
{
    
    self.selectYear  = [self.pickerView selectedRowInComponent:0] + self.minShowYear;
    
    self.selectMonth = [self.pickerView selectedRowInComponent:1] + 1;
    if (_isShowDay==YES) {
        self.selectDay   = [self.pickerView selectedRowInComponent:2] + 1;
        
        if (_isShowSecond&&_isShowMin) {
            self.selectMin =[self.pickerView selectedRowInComponent:4] + 1;
            self.selectSec =[self.pickerView selectedRowInComponent:5] + 1;
        }
    }
    if (_isShowHour) {
        self.selectHour =[self.pickerView selectedRowInComponent:3] + 1;
    }
    
}


- (void)setYearLeast:(NSInteger)yearLeast
{
    _minShowYear = yearLeast;
}

- (void)setYearSum:(NSInteger)yearSum
{
    _yearSum = yearSum;
}
//是否显示日信息
-(void)setIsShowDay:(BOOL)isShowDay{
    _isShowDay=isShowDay;
}
-(void)setIsShowSecond:(BOOL)isShowSecond{
    _isShowSecond=isShowSecond;
}
-(void)setIsShowMin:(BOOL)isShowMin{
    _isShowMin = isShowMin;
}

@end
